<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>租户等级设置</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入等级名称" clearable v-model="userGrage" @clear="searchUser">
            <el-button slot="append" icon="el-icon-search" @click="searchUser"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible=true">新增等级</el-button>
        </el-col>
      </el-row>
      <el-table :data="glist" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="等级名称" prop="grade"></el-table-column>
        <el-table-column label="默认租户等级">

          <template slot-scope="scope">
            <span v-text="scope.row.defaultState == '0'?'默认':'设置默认'"></span>
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="warning" size="mini" @click="showEditDialog(scope.row)">修改</el-button>
            <el-button type="danger" size="mini" @click="removeUserById(scope.row.id)" :disabled="scope.row.defaultState == 0">删除</el-button>

          </template>
        </el-table-column>
      </el-table>



      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[3, 6 , 12, 24]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      ></el-pagination>
    </el-card>
    <!--添加用戶-->
    <el-dialog title="添加用戶" :visible.sync="addDialogVisible" width="50%" @close="addUserFormClose">
      <el-form
          :model="addUserForm"
          :rules="addUserFormRules"
          ref="addUserFormRef"
          label-width="100px"
          class="demo-ruleForm"
      >

        <el-form-item label="等级名称" prop="grade">
          <el-input v-model="addUserForm.grade"></el-input>
        </el-form-item>

        <el-form-item label="租户等级默认" prop="defaultState">
          <el-select v-model="addUserForm.defaultState" placeholder="请选择是设置此等级为默认等级">
            <el-option label="否" :value="1"></el-option>
            <el-option label="是" :value="0"></el-option>
          </el-select>
        </el-form-item>


      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addUser">确 定</el-button>
        <el-button @click="cancelAddRole">取 消</el-button>
      </span>
    </el-dialog>

    <!--修改用戶-->
    <el-dialog
        title="修改用戶"
        :visible.sync="editDialogVisible"
        width="50%"
        @close="editUserFormClose"
    >
      <el-form
          :model="editUserForm"
          :rules="editUserFormRules"
          ref="editUserFormRef"
          label-width="100px"
          class="demo-ruleForm"
      >

        <el-form-item label="等级名称" prop="grade">
          <el-input v-model="editUserForm.grade"></el-input>
        </el-form-item>

        <el-form-item label="租户等级默认" prop="defaultState">
          <el-select v-model="editUserForm.defaultState" placeholder="请选择是设置此等级为默认等级">
            <el-option label="否" :value="1"></el-option>
            <el-option label="是" :value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click.prevent="editUser">确 定</el-button>
        <el-button @click="canceleditRole">取 消</el-button>
      </span>
    </el-dialog>

  </div>
</template>
<script>
export default {
  data() {
    return {

      userGrage: "",
      glist:[],
      gid:"",
      editDialogVisible: false,
      addDialogVisible: false,
      roleIds:[], //存储选中的h角色ID
      pageNum: 1,
      pageSize: 3,
      userList: [],
      swflag: false,
      total: 9,
      addUserForm: {
        grade:"",
        defaultState:"",

      },
      editUserForm: {
        id: -1,
        grade:"",
        defaultState:"",

      },
      editUserFormRules: {
        grade: [
          { required: true, message: "等级名称必填", trigger: "blur" },
          { min: 3, max: 8, message: "名称在3到8个字符之间", trigger: "blur" }
        ],
        defaultState: [
          { required: true, message: "是否设为默认必填", trigger: "blur" },
        ],

      },
      addUserFormRules: {
        grade: [
          { required: true, message: "等级名称必填", trigger: "blur" },
          { min: 3, max: 8, message: "用户名在3到8个字符之间", trigger: "blur" }
        ],
        defaultState: [
          { required: true, message: "是否设为默认必填", trigger: "blur" },
        ],

      }
    };
  },
  methods: {


    initGrade(){
      let url = "/grade/list";
      this.$axios.get(url,{
        params:{
          now:this.pageNum,
          size:this.pageSize,
          name:this.userGrage
        }
      }).then(res=>{
        if(res.data.code == 200){
          this.glist = res.data.data.list ;
          this.pageSize = res.data.data.pageSize;
          this.pageNum = res.data.data.pageNum ;
          this.total = res.data.data.total ;
        }else {
          this.$message({
            type:'error',
            message:res.data.msg,
            duration:1000
          })
        }
      })

    },
    searchUser() {
      this.initGrade();
    },
    showEditDialog(row) {
      this.editDialogVisible = true ;
      this.editUserForm=row;
      console.log(row);

    },
    editUser() {
      this.$refs.editUserFormRef.validate(valid=>{
        if (!valid) {
          return;
        }
        let url = "/grade/modify" ;
        this.$axios.put(url,this.editUserForm)
            .then(res=>{
              if (res.data.code == 200){
                this.editDialogVisible = false;
                this.initGrade();

                this.$message({
                  type:"success",
                  message:res.data.msg,
                  duration:1000
                })

              }else {
                this.$message({
                  type:"error",
                  message:res.data.msg,
                  duration:1000
                })
              }
            })
      })

    },
    canceleditRole(){
      this.editDialogVisible = false;
      this.initGrade();
    },
    editUserFormClose() {
      this.editDialogVisible = false ;
      this.$refs.editUserFormRef.resetFields();
      this.initGrade();
    },
    addUserFormClose() {
      this.addDialogVisible = false ;
      this.$refs.addUserFormRef.resetFields();
      this.initGrade();
    },

    //取消添加
    cancelAddRole(){
      this.addDialogVisible = false ;
      this.$refs.addUserFormRef.resetFields();
      this.initGrade();

    },
    addUser() {
      this.$refs.addUserFormRef.validate(valid=> {
        if (!valid) {
          return;
        }
        let url = "/grade/add";
        this.addUserForm.roleIds = this.roleIds;
        this.$axios.post(url, this.addUserForm)
            .then(res => {
              if (res.data.code == 200) {
                this.addDialogVisible = false;
                this.$refs.addUserFormRef.resetFields();
                this.$message({
                  type:"success",
                  message:res.data.msg,
                  duration:1000
                })
                this.initGrade();

              } else {
                this.$message({
                  type: 'error',
                  message: res.data.msg,
                  duration: 1000
                })
              }
            })
      })
    },
    handleSizeChange(newSize) {
      this.pageSize = newSize;
      this.pageNum = 1;
      this.initGrade();
    },
    handleCurrentChange(newPage) {
      this.pageNum = newPage;
      this.initGrade();
    },

    //物理删除
    removeUserById(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = "/grade/del/"+id ;
        this.$axios.delete(url)
            .then(res=>{
              if (res.data.code == 200){
                this.$message({
                  type:"success",
                  message:res.data.msg,
                  duration:1000
                })
                this.initGrade();
              }else {
                this.$message({
                  type:"error",
                  message:res.data.msg,
                  duration:1000
                })
              }
            })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },
/*    getRoleNames(list){
      let info = '';
      if (list != null && list.length >0){
        for (let r of list){
          info += r.name +","
        }
      }
      return info.substring(0,info.length-1) ;
    },*/
  },
  created() {
    this.initGrade();

  }
};
</script>
<style lang="less" scoped>
</style>